<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="./js//local.js"></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="keyword" @keyup.enter="handleEnter">
        <h2>历史搜索 <button @click="handleAll">全部删除</button></h2>
        <p v-for="item of history">{{item}} <button @click="handleDelete(item)">删除</button></p>
    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                keyword:"",
                history:[]
            },
            methods: {
                handleEnter(){
                    if(!this.history.includes(this.keyword)){
                        this.history.unshift(this.keyword)
                        setLocalStroage('historys',this.history)
                    }else{
                        var index = this.history.indexOf(this.keyword);
                        this.history.splice(index,1)
                        this.history.unshift(this.keyword)
                    }
                    this.keyword = ""
                },
                handleDelete(val){
                    var index = this.history.indexOf(val);
                    this.history.splice(index,1)
                    setLocalStroage('historys',this.history)
                },
                handleAll(){
                    this.history="";
                    setLocalStroage('historys',this.history)
                }
            },
            mounted() {
                var historys = getLocalStroage('historys');
                if(historys){
                    this.history = historys;
                }
            },
        })
    </script>
</body>
</html>